<?php

namespace App\Http\Controllers\User;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Map\Goods;
use App\Models\UserBankList;
use Illuminate\Http\Request;
// 用户绑定银行卡
class UserBankListController extends Controller
{
    use ResponseData;
    public function userBankList(UserBankList $model,Request $request){
        $data = $model->query()->where('openid',$request->openid)->get();
        return $this->success($data);
    }
    public function list(UserBankList $model,Request $request){
        $size = $request->input('size',10);
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        $model = $model->orderBy('sort');
        if($request->filled('limit')&&$request->limit=='all'){
            return $this->success($model->get()) ;
        }else{
            $data = $model->paginate($size);
            return $this->success($data);
        }
    }
    public function store(UserBankList $model,Request $request){
        $map = $model->where('openid',$request->openid)->where('enc_bank_no',$request->enc_bank_no)->first();
        if($map){
            return $this->failed('该卡号已经存在,请勿重复添加',201);
        }
        $data = $request->all();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    public function update(UserBankList $model,Request $request){
        if(!$request->openid){
            return $this->failed('用户不存在',201);
        }
        $data = $model->where('openid',$request->openid)->find($request->id);
        $res = $request->all();
        $data->update($res);
        return $this->success('修改成功');
    }
    //编辑
    public function edit(UserBankList $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }

}
